package cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformauth;

import java.util.*;
import java.time.LocalDateTime;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformauth.PlatformAuthDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformauth.vo.*;

/**
 * 跨境电商平台授权信息 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface PlatformAuthMapper extends BaseMapperX<PlatformAuthDO> {

    default PageResult<PlatformAuthDO> selectPage(PlatformAuthPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PlatformAuthDO>()
                .eqIfPresent(PlatformAuthDO::getPlatformId, reqVO.getPlatformId())
                .eqIfPresent(PlatformAuthDO::getStoreId, reqVO.getStoreId())
                .eqIfPresent(PlatformAuthDO::getAuthType, reqVO.getAuthType())
                .eqIfPresent(PlatformAuthDO::getClientId, reqVO.getClientId())
                .eqIfPresent(PlatformAuthDO::getClientSecret, reqVO.getClientSecret())
                .eqIfPresent(PlatformAuthDO::getAccessToken, reqVO.getAccessToken())
                .eqIfPresent(PlatformAuthDO::getRefreshToken, reqVO.getRefreshToken())
                .eqIfPresent(PlatformAuthDO::getTokenType, reqVO.getTokenType())
                .eqIfPresent(PlatformAuthDO::getScope, reqVO.getScope())
                .eqIfPresent(PlatformAuthDO::getExpiresIn, reqVO.getExpiresIn())
                .betweenIfPresent(PlatformAuthDO::getTokenExpireTime, reqVO.getTokenExpireTime())
                .eqIfPresent(PlatformAuthDO::getApiKey, reqVO.getApiKey())
                .eqIfPresent(PlatformAuthDO::getApiSecret, reqVO.getApiSecret())
                .eqIfPresent(PlatformAuthDO::getAdditionalParams, reqVO.getAdditionalParams())
                .eqIfPresent(PlatformAuthDO::getAuthStatus, reqVO.getAuthStatus())
                .betweenIfPresent(PlatformAuthDO::getAuthTime, reqVO.getAuthTime())
                .betweenIfPresent(PlatformAuthDO::getLastRefreshTime, reqVO.getLastRefreshTime())
                .eqIfPresent(PlatformAuthDO::getAuthErrorMsg, reqVO.getAuthErrorMsg())
                .eqIfPresent(PlatformAuthDO::getAutoRefresh, reqVO.getAutoRefresh())
                .betweenIfPresent(PlatformAuthDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(PlatformAuthDO::getId));
    }

    default PlatformAuthDO selectByStoreId(Long storeId) {
        return selectOne(PlatformAuthDO::getStoreId, storeId);
    }

    default List<PlatformAuthDO> selectByPlatformId(Long platformId) {
        return selectList(PlatformAuthDO::getPlatformId, platformId);
    }

    default List<PlatformAuthDO> selectExpiredAuth(LocalDateTime expireTime) {
        return selectList(new LambdaQueryWrapperX<PlatformAuthDO>()
                .lt(PlatformAuthDO::getTokenExpireTime, expireTime)
                .eq(PlatformAuthDO::getAuthStatus, 1)); // 只查询已授权状态的
    }

    default List<PlatformAuthDO> selectByAuthStatus(Integer authStatus) {
        return selectList(PlatformAuthDO::getAuthStatus, authStatus);
    }

    default List<PlatformAuthDO> selectByAutoRefresh(Boolean autoRefresh) {
        return selectList(PlatformAuthDO::getAutoRefresh, autoRefresh);
    }

}